layui.use(['table','laypage'], function(){
  var table = layui.table;
  var curr_page = 0;
	/*初始化表格*/
	function tableInit()
	{  
	  table.render({
		elem: '#dataTables-example'
		,url:'/admin/account/adminlist.html'
		,method:'POST'
		,parseData: function(res){ //res 即为原始返回的数据
			//封装返回数据
			console.log(res);
			var urls = '';
			curr_page = res.current_page;
			
			for (i in res.data) {
				if(res.data[i]['admin_id'] == 1)
				{
					res.data[i]['reason'] = "";
				}else{
					urls = '/admin/Account/editadmin/id/'+res.data[i]['admin_id']+'.html';
					res.data[i]['reason'] = "<button class='layui-btn' onclick=\"xadmin.open('修改规则','"+urls+"',800,500)\">编辑</button>";
					res.data[i]['reason'] += "<button class='layui-btn delAdmin' aid='"+res.data[i]['admin_id']+"' >删除</button>";
				}
			}
			return {
			  "code": 10001,
			  "msg": '',
			  "count": res.total,
			  "data": res.data
			};
		}
		,cols: [[
		  {field:'admin_id', title: '序号'}
		  ,{field:'addtime', title: '创建时间'} 
		  ,{field:'account', title: '账号'}
		  ,{field:'shop_id', title: '部门'}
		  ,{field:'nkname', title: '操作员工'}
          ,{field:'phone', title: '联系电话'}
		  ,{field:'admin_type', title: '职位'}
		  ,{field:'remarks', title: '备注'}
		  ,{field:'reason', title: '操作'}
		]]
		,page: {
		  layout: ['prev', 'page', 'next', 'skip', 'count']
		  ,curr:curr_page
		}
		,response: {
			statusCode: 10001
		}
	  });
	};
	tableInit();
	  //删除请假
	$('body').on('click', '.delAdmin', function(){
		var get_this = $(this);
		parent.layer.confirm('你确定要删除该条信息吗?', function(){
			var params = {};
			params.id = get_this.attr('aid');
			$.ajax({
				type: "POST",
				url: "/admin/account/deladmin.html",
				cache: false, //禁用缓存
				data: params, //传入组装的参数
				dataType: "json",
				success: function (result) {
					if(result.code==10001){
						tableInit();
						parent.layer.msg('操作成功', {icon: 1});
					}else{
						parent.layer.msg(result.msg, {icon: 5});
					}
				},
				error:function () {
					parent.layer.msg('网络错误', {icon: 5});
				}
			})
		});
	});
});

